
#include "Interactiv/BMFont.h"

 BMFont::BMFont()
{	
	 m_fontbits = NULL;
}

 BMFont::~BMFont()
{
	assert( m_fontbits == NULL );
}
 
bool BMFont::open(const char *filename)
{
	assert( m_fontbits == NULL );
	
	FILE *fp = fopen(filename,"rb");
	if(fp==NULL)
		return false;
	
	fread(&m_header,1,sizeof(m_header),fp);
	m_numOfFont = m_header.width*m_header.height/8;
	const int bufsize = m_numOfFont * ((0XF7-0XA1+1)*94);
	m_fontbits = new unsigned char[bufsize];
	fread(m_fontbits,1,bufsize,fp);
	
	fclose(fp);
	return true;
}

void BMFont::close()
{
	assert( m_fontbits != NULL );
	delete []m_fontbits;
	m_fontbits = NULL;
}

 unsigned int BMFont::getWidth()
{
	return m_header.width;
}

 unsigned int BMFont::getHeight()
{
	return m_header.height;
}

const unsigned char* BMFont::getBits(const char *c)
{
	const unsigned char *_c = (const unsigned char*)c;
	return m_fontbits + m_numOfFont*((_c[0]-0XA1)*94 + _c[1]-0xA1);
}

const unsigned char* BMFont::getBits(unsigned short c)
{
	int num = c>>8 - (unsigned short)0XA1;
	int area = (c & 0X00FF) - (unsigned short)0XA1;
	int index = area*94 + num;
	return m_fontbits + index * m_numOfFont;
}


